/**
 * Created by cuppi on 2016/12/14.
 */

class UrlTool {
  static urlFromPortion(baseUrl, subUrl, parameters) {
    if (!parameters) {
      parameters = {};
    }
    let paras = [];
    for (let key in parameters) {
      if (!parameters.hasOwnProperty(key)) {
        continue;
      }
      if (parameters[key] !== undefined) {
        paras.push(key + '=' + parameters[key]);
      }
    }

    let iUrl = baseUrl + subUrl;
    if (paras.length > 0) {
      iUrl = iUrl + '?' + paras.join('&');
    }
    return iUrl;
  }

  static getQueryString (key) {
    var reg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)')
    var r = window.location.search.substr(1).match(reg)
    if (r != null) {
      return unescape(r[2])
    }
    return null
  }

  static getNowUrl() {
    let nowUrl = window.location.href;
    nowUrl = nowUrl.substring(0, nowUrl.indexOf('#/'));
    return nowUrl
  }
}

export default UrlTool;
